//
//  RepairShowVCViewController.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/29.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RepairShowVC.h"
#import "Sizes.h"
#import "ZFUtilities.h"
#import <AVFoundation/AVFoundation.h>
#import "UIButton+AFNetworking.h"
#import "ZFNormalViewController.h"
#import "UIImageView+YPImage.h"
#import "MagicImageView.h"
#import "EquipmentService.h"
#import "UIView+ZFFrame.h"

@interface RepairShowVC ()

@property(nonatomic,strong) RepairsApplyModel *repairModel;
@property(nonatomic,strong) UIScrollView *scrollView;

@property(nonatomic,weak) IBOutlet UIView *stateView;
@property(nonatomic,weak) IBOutlet  UILabel *stateLab;
@property(nonatomic,weak) IBOutlet  UILabel *equipNameLab;
@property(nonatomic,weak) IBOutlet  UILabel *timeLab;

@property(nonatomic,weak) IBOutlet UIView *infoView;
@property(nonatomic,weak) IBOutlet  UILabel *contactsLab;
@property(nonatomic,weak) IBOutlet  UILabel *mobileLab;
@property(nonatomic,weak) IBOutlet  UILabel *locationLab;
@property(nonatomic,weak) IBOutlet  UILabel *addressLab;
@property(nonatomic,weak) IBOutlet UILabel *questLab;
@property(nonatomic,weak) IBOutlet MagicImageView *imageView;
@property(nonatomic,weak) IBOutlet UIButton *videoActionBtn;

@property(nonatomic,weak) IBOutlet UIView *bottomView;
@property(nonatomic,weak) IBOutlet UIButton *cancelBtn;

@property(nonatomic,copy) EquipmentService *services;
@end

@implementation RepairShowVC

-(instancetype)initWithShow:(RepairsApplyModel*)repairModel{
    self = [super init];
    if (self) {
        self.repairModel = repairModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"报修详情"];
    
    _scrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-SafeBottomMargin)];
    [self.view addSubview:_scrollView];
    
    _stateView.frame = CGRectMake(0, 0, ScreenWidth, _stateView.zf_height);
    [self.scrollView addSubview:_stateView];
    
    _infoView.frame = CGRectMake(0, _stateView.zf_bottom, ScreenWidth, _infoView.zf_height);
    [self.scrollView addSubview:_infoView];
    
    _bottomView.frame = CGRectMake(0, _infoView.zf_bottom, ScreenWidth, _bottomView.zf_height);
    [self.scrollView addSubview:_bottomView];
    
    //[_videoActionBtn setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:_repairModel.videoUrl]];
    
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 5;
    
    _cancelBtn.layer.masksToBounds = YES;
    _cancelBtn.layer.cornerRadius = 5;
    
    self.scrollView.contentSize= CGSizeMake(ScreenWidth, _bottomView.zf_bottom);
    
    [self setUIData];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(void)setUIData{
    self.stateLab.text = [self.repairModel getStatusStr];
    self.equipNameLab.text = self.repairModel.equipmentName;
    self.timeLab.text = self.repairModel.createTime;
    self.contactsLab.text = self.repairModel.userName;
    self.mobileLab.text = self.repairModel.mobile;
    self.locationLab.text = [NSString stringWithFormat:@"%@%@%@",self.repairModel.province,self.repairModel.city,self.repairModel.district];
    self.addressLab.text = self.repairModel.address;
    self.questLab.text = self.repairModel.applyDesc;
    
    [self.imageView yp_setImageUlr:[NSString stringWithFormat:@"%@%@",BaseImageUrl,_repairModel.imageUrl] placeHolder:@""];
    
    if(_repairModel.videoUrl.length){
        UIImage *videoImage = [self thumbnailImageForVideo:[NSURL URLWithString:[BaseImageUrl stringByAppendingString:  _repairModel.videoUrl]] atTime:1.0];
        [self.videoActionBtn setBackgroundImage:videoImage forState:UIControlStateNormal];
        [self.videoActionBtn setImage:ZFPlayer_Image(@"ZFPlayer_play") forState:UIControlStateNormal];
    }
    
    if (self.repairModel.status == 9) {
        [self.bottomView removeFromSuperview];
    }
}

-(IBAction)cancelAction:(id)sender{
    [self.services getcancelEquipmentrepairsapplyID:self.repairModel.id completion:^(LMJBaseResponse * _Nonnull respons) {
        
        if (respons.message) {
            [WHToast showMessage:respons.message duration:2 finishHandler:nil];
        }else{
            [WHToast showMessage:@"取消成功" duration:2 finishHandler:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

-(IBAction)videoAction:(id)sender{
    ZFNormalViewController *douYinVC = [[ZFNormalViewController alloc] initWithUrl:self.repairModel.videoUrl];
    [self.navigationController pushViewController:douYinVC animated:YES];
}


-(UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {  AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

    NSParameterAssert(asset);

　AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
     assetImageGenerator.appliesPreferredTrackTransform = YES;
　 assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
      CGImageRef thumbnailImageRef = NULL;
　CFTimeInterval thumbnailImageTime = time;
　NSError *thumbnailImageGenerationError = nil;
　   thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)

NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);

  UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;

　return thumbnailImage;

}

@end
